/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const _ = require("./UpdateCanvasInput.js")

const { Scr } = require("../engine/XDefine.js");
const { GLFrame } = require("../engine/GLFrame.js");
const { XTexture } = require("../engine/graphics/XTexture.js");
const { XCenter } = require("./XCenter.js");
const { X2DBeta } = require("../engine/graphics/X2DInterface.js");
const { XTools } = require("../engine/XTools.js");
const { XFont } = require("../engine/graphics/XFont.js");

window.G = {}

let canvas;
let windowWidth, windowHeight;

function ResizeClient() {
    let dpr = window.devicePixelRatio;
    XTools.GLOBAL_DPR = dpr;
    // XTools.GLOBAL_STR += `${document.documentElement.clientWidth},${document.documentElement.clientHeight}=`;
    // XTools.GLOBAL_STR += `${window.innerWidth},${window.innerHeight}`;
    // windowWidth = document.documentElement.clientWidth;
    // windowHeight = document.documentElement.clientHeight;
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;
    console.log("a reset dpr", dpr);
    let dw = Math.round(windowWidth * dpr);
    let dh = Math.round(windowHeight * dpr);
    Scr.SetLogicScreenSize(windowWidth, windowHeight, dw, dh);
    canvas.width = Math.round(dw);
    canvas.height = Math.round(dh);
    canvas.style.width = windowWidth + 'px';
    canvas.style.height = windowHeight + 'px';
    XTexture.Init(dpr);
}

function mydraw() {
    if (XTools.GLOBAL_DPR != window.devicePixelRatio ||
        windowWidth != window.innerWidth ||
        windowHeight != window.innerHeight) {
        ResizeClient();
        // GLFrame.gi().resize();
    }
    X2DBeta.COUNT_FLUSH = 0;
    X2DBeta.swapMode2D();

    // X2DBeta.drawText("0", 50, 0, 0, 1, 1, 0, -1, -1, 0xffffffff);
    // X2DBeta.drawText("1", 50, 0, Scr.logich, 1, 1, 0, -1, -3, 0xffffffff);
    // X2DBeta.drawText("2", 50, Scr.logicw, Scr.logich, 1, 1, 0, -3, -3, 0xffffffff);
    // X2DBeta.drawText("3", 50, Scr.logicw, 0, 1, 1, 0, -3, -1, 0xffffffff);
    // X2DBeta.drawText("abcdefghijklmnopqrstuvwxyz", 50, Scr.logicw / 2, Scr.logich / 2, 1, 1, 0, -2, -2, 0xffffffff);
    XCenter.gi().onDraw();
    // X2DBeta.drawRect(100,100,100,100,0xffffffff,1);
    // X2DBeta.drawLine(100,100,200,200,0xffff0000,10);
    // X2DBeta.drawRect(102,102,96,96,0xffffffff,1);
    XTexture._FreshText();

    X2DBeta.Flush();

    // X2DBeta.drawText("" + X2DBeta.COUNT_FLUSH, 20, Scr.logicw / 2, 0, 1, 1, 0, -2, -1, 0xffffffff);
    // X2DBeta.Flush();
}

function mytouch(msg, x, y) {
    XCenter.gi().onTouch(msg, x, y)
}

function mykey(type, code) {
    XCenter.gi().onKey(type, code)
}

window.onload = () => {
    canvas = document.getElementById("visual_area");
    ResizeClient();

    if (XTools.isWindows()) {
        XFont.WaitFontLoaded(() => {
            GLFrame.gi().Go(canvas, mydraw, mytouch, mykey);
        });
    }
    else {
        GLFrame.gi().Go(canvas, mydraw, mytouch, mykey);
    }

}
